package com.moqianfan.mcuassistant.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.moqianfan.mcuassistant.R;
import com.moqianfan.mcuassistant.adapter.CommDataAdapter;
import com.moqianfan.mcuassistant.adapter.ExpandMcuOrderAdapter;
import com.moqianfan.mcuassistant.bean.McuOrder;
import com.moqianfan.mcuassistant.database.McuOrderDao;
import com.moqianfan.mcuassistant.utils.LogUtil;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.OnClick;

public class OrderActivity extends BaseActivity {

    @BindView(R.id.exp_list)
    ExpandableListView exlistMcuOrder;
    @BindView(R.id.image_del)
    ImageView image_delete;
    @BindView(R.id.edit_search)
    EditText edit_search;
    @BindView(R.id.list_search)
    ListView list_search;
    @BindView(R.id.txt_order)
    TextView txtOrder;

    private List<McuOrder> allOrder;
    private List<String> heads;
    private List<List<McuOrder>> childOrder;
    private McuOrderDao mcuOrderDao;
    private ExpandMcuOrderAdapter adapter;
    private CommDataAdapter<McuOrder>searchAdapter;
    private List<McuOrder> searchOrder;


    private String TAG = "OrderActivity";

    @Override
    public void findViews() {
        setContentView(R.layout.activity_order);
    }

    @Override
    public void initViews() {
        heads = new ArrayList<>();
        childOrder = new ArrayList<>();
        allOrder = new ArrayList<>();
        searchOrder = new ArrayList<>();
        mcuOrderDao = appConfig.getDaoSession().getMcuOrderDao();
        edit_search.addTextChangedListener(watcher);
        txtOrder.setMovementMethod(new ScrollingMovementMethod());
    }

    @Override
    public void initDatas() {
        allOrder = mcuOrderDao.loadAll();
        Set<String> temp = new LinkedHashSet<>();
        for (McuOrder item : allOrder) {
            temp.add(item.getOrderType());
            LogUtil.d(TAG, item.toString());
        }
        heads.clear();
        heads.addAll(temp);
        LogUtil.d(TAG, "HEAD:" + heads.size());
        List<McuOrder> tempMcus;
        childOrder.clear();
        for (String item : heads) {
            LogUtil.w(TAG, "章节：" + item);
            tempMcus = new ArrayList<>();
            for (McuOrder item1 : allOrder) {
                if (item1.getOrderType().equals(item))
                    tempMcus.add(item1);
            }
            childOrder.add(tempMcus);
        }

        if (null == adapter) {
            adapter = new ExpandMcuOrderAdapter(heads, childOrder, this);
            exlistMcuOrder.setAdapter(adapter);
        } else {
            adapter.setgData(heads);
            adapter.setiData(childOrder);
        }
    }

    private TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (!TextUtils.isEmpty(s.toString())) {
                list_search.setVisibility(View.VISIBLE);
                image_delete.setVisibility(View.VISIBLE);
                searchOrder.clear();
                for (McuOrder item:allOrder){
                    if (item.getOrderType().contains(s.toString())||item.getOrderName().contains(s.toString())||
                            item.getOrderFunction().contains(s.toString()))
                        searchOrder.add(item);
                }
                if (null==searchAdapter){
                    searchAdapter=new CommDataAdapter<McuOrder>(searchOrder,R.layout.view_item_order) {
                        @Override
                        public void bindView(ViewHolder holder, McuOrder obj) {
                            holder.setText(R.id.txt_order_1,String.format("格式：%s", obj.getOrderName()));
                            holder.setText(R.id.txt_order_2,String.format("周期数：%s  字节数：%s", obj.getOrderCycle(),
                                    obj.getOrderSize()));
                            holder.setText(R.id.txt_order_3,String.format("功能：%s", obj.getOrderFunction()));
                        }
                    };
                    list_search.setAdapter(searchAdapter);
                }else {
                    searchAdapter.setmData(searchOrder);
                }
            } else {
                list_search.setVisibility(View.GONE);
                image_delete.setVisibility(View.GONE);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    @OnClick({R.id.image_close, R.id.image_del})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image_close:
                finish();
                break;
            case R.id.image_del:
                edit_search.setText("");
                break;
        }
    }
}
